import React, {useState} from 'react';
import './index.less'
import SysDepart, {SysDepartTree} from "@/api/sys-depart";
import {useEdit, useHandle} from "@/common/hooks";
import {Form, Space} from 'antd';
import HButton from "@/components/ant-ext/HButton";
import GetSelectNode, {HSearchTree} from "@/components/ant-ext/HSearchTree";
import {DropHandle} from "@/components/ant-ext/HSearchTreeExtDropHandle";
import HFormItem from "@/components/ant-ext/HFormItem";
import HAction from "@/components/common/HAction";
import {HNotification} from "@/config/route/route-func";

export default function Index() {
    // const [treeData, setTreeData] = useState<SysDepartTree[]>([]);
    // const [loading, setLoading] = useState(false)
    // const [editLoading, setEditLoading] = useState(false)
    // const [currentNode, setCurrentNode] = useState<SysDepartTree>();
    // const [form] = Form.useForm();
    // const onSelect = (selectedKeys: React.Key[], info: any) => {
    //     const nodes = GetSelectNode(treeData, info.node)
    //     const currentNode = nodes[nodes.length - 1] as SysDepartTree;
    //     setCurrentNode(currentNode)
    //     form.setFieldsValue({
    //         pCode: currentNode.pId,
    //         orgCode: currentNode.key,
    //         orgName: currentNode.title,
    //     })
    // };
    // const reload = useHandle(() => SysDepart.treeView({setLoading, success: setTreeData}), [])
    // const saveAndFlush = useEdit(SysDepart.doEdit, {
    //     setLoading: setEditLoading,
    //     success: () => {
    //         form.resetFields(["orgCode", "orgName"])
    //         reload()
    //     }
    // })
    // const layout = {
    //     form: form
    // }

    return (<>
        {/* <div className="parent">
            <div className={"searchSelect"}>
                <Space style={{marginBottom: "10px"}}>
                    <HButton type={"primary"} onClick={() => {
                        form.resetFields()
                        form.setFieldValue("pCode", "ROOT")
                        setCurrentNode(undefined)
                    }}> 添加一级节点</HButton>
                    <HButton onClick={() => {
                        if (currentNode) {
                            form.resetFields()
                            setCurrentNode(undefined)
                            form.setFieldValue("pCode", currentNode?.key)
                        } else {
                            HNotification.warning({message: "请先选择父级菜单"})
                        }
                    }}> 添加子节点</HButton>
                    <HAction action={(setLoading) => {
                        SysDepart.doResort({setLoading, success: reload})
                    }}> 顺序重编</HAction>
                </Space>
                <HSearchTree blockNode showLine showIcon={false}
                             loading={loading}
                             reload={reload}
                             treeData={treeData} onSelect={onSelect} draggable
                             onDrop={(handle) => {
                                 let dropHandle = handle as DropHandle
                                 let sourcePos = dropHandle.sourcePos.split("-")
                                 let targetPos = dropHandle.targetPos.split("-")
                                 if (sourcePos.length !== targetPos.length || sourcePos[sourcePos.length - 2] !== targetPos[targetPos.length - 2]) {
                                     HNotification.warning({message: "只允许平级移动"})
                                 } else {
                                     let sourceIndex = Number(sourcePos[sourcePos.length - 1])
                                     let targetIndex = Number(targetPos[targetPos.length - 1])
                                     if (sourceIndex < targetIndex) {
                                         targetIndex = targetIndex - 1
                                     }
                                     SysDepart.doMove({
                                         body: {
                                             sourceCode: (dropHandle.parent?.children! || dropHandle.treeData)[sourceIndex].key,
                                             targetCode: (dropHandle.parent?.children! || dropHandle.treeData)[targetIndex].key
                                         },
                                         setLoading, success: reload
                                     })
                                 }
                             }}/>

            </div>
            <div className={"menuForm"}>
                <Form form={form} labelCol={{sm: 6}} onFinish={() => {
                    saveAndFlush({body: {...form.getFieldsValue(), add: currentNode === undefined}})
                }}>
                    <HFormItem {...layout} label={"父级节点"} name={"pCode"} disabled={true}/>
                    <HFormItem {...layout} label={"组织编码"} name={"orgCode"} disabled={true} />
                    <HFormItem {...layout} label={"组织名称"} name={"orgName"} required={true}/>

                    <Space style={{marginLeft: "200px"}}>
                        <HButton loading={editLoading} type={"primary"} onClick={form.submit}>保存 </HButton>
                        <HAction disabled={currentNode === undefined} confirm={{
                            title: "是否删除[" + currentNode?.title + "]",
                            ok: (setLoading) => {
                                SysDepart.deleteById({
                                    body: {code: currentNode?.key}, setLoading, success: () => {
                                        setCurrentNode(undefined)
                                        reload()
                                        form.resetFields()
                                    }
                                })
                            },
                        }}
                        >
                            删除
                        </HAction>
                    </Space>

                </Form>

            </div>
        </div> */}
    </>);
};
